import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { BaseHttpService } from '../../../shared/services/baseHttp.service';
import { Observable } from 'rxjs/Observable';
import { StorageService } from '../../../shared/services/storage.service';
import { ChartPointModel } from '../../../shared/models/chartPoint.model';
import { ClusterModelWrapper } from '../riskCluster/cluster.model';
import { CompanyCountModel } from '../../../shared/models/companyCount.model';
import { EsgListModel } from './models/esgList.model';
import { EsgFilterModel } from './models/esgFilter.model';

/**
 * Overall Esg rating main service
 * 
 * Author: Andrei Istrate
 * Date: 20.03.2018
 */
@Injectable()
export class ResearchEsgRatingService extends BaseHttpService {
    public static clusters: Promise<ClusterModelWrapper[]>;

    constructor(private http: HttpClient) {
        super();
    }

    /**
     * Get list of companies with risk rating research
     * @param filters Risk list filter object
     * @param skip Index to take from
     * @param take Number of companies to take
     * @param orderBy Name of column to order by
     * @param orderByAsc True to order ascending by the 'orderBy' column or false for descending
     */
    public getCompanies(filters: EsgFilterModel, skip: number, take: number, orderBy: string, orderByAsc: boolean): any {
        let companies: EsgListModel[] = [];
        let params: HttpParams = new HttpParams();
        params = params.set("skip", skip.toString())
            .set("take", take.toString())
            .set('getCount', 'false')
            .set('orderBy', orderBy)
            .set('orderAscending', orderByAsc.toString());
        params = this.urlEncodeObject(filters, params, 'companyName');    
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<EsgListModel[]>(this.serverUrl + 'esgratingserv/api/v1/companies/esglisting?companyName=' + this.paramEncode(filters, 'companyName'), { params: params });

    }

    /**
     * Get number of companies after the input filter is applied
     * @param filters Risk list filter object
     */
    public getCompaniesCount(filters: EsgFilterModel): Observable<CompanyCountModel> {
        let params: HttpParams = new HttpParams();

        params = params.set("getCount", 'true')
        params = this.urlEncodeObject(filters, params, 'companyName');
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<CompanyCountModel>(this.serverUrl + 'esgratingserv/api/v1/companies/esglisting?companyName=' + this.paramEncode(filters, 'companyName'), { params: params });
    }

    /**
    * Get Esg Summary Performance Assessments Lists
    */
    public getEsgPerfomanceAssessmentsList(): Observable<any[]> {
        let params: HttpParams = new HttpParams();
        params = params.set("nomenclature", "assessments");
        return this.http.get<any[]>(this.serverUrl + 'esgratingserv/api/v1/companies/esglisting/nomenclaturesList', { params: params });
    }

    /**
     * Get Sub Industries Lists
     */
    public getSubIndustriesList(): Observable<any[]> {
        let params: HttpParams = new HttpParams();
        params = params.set("nomenclature", "subIndustries");
        return this.http.get<any[]>(this.serverUrl + 'esgratingserv/api/v1/companies/esglisting/nomenclaturesList', { params: params });
    }
}